<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 人机交互作业管理系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .login-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 450px;
            padding: 40px;
            text-align: center;
        }
        
        .logo {
            margin-bottom: 30px;
        }
        
        .logo h1 {
            color: #333;
            font-size: 2rem;
            margin-bottom: 10px;
        }
        
        .logo .subtitle {
            color: #666;
            font-size: 1.1rem;
        }
        
        .school-name {
            color: #2575fc;
            font-weight: bold;
        }
        
        .course-name {
            color: #ff6b6b;
            font-weight: bold;
        }
        
        .form-group {
            margin-bottom: 25px;
            text-align: left;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5ee;
            border-radius: 8px;
            font-size: 1rem;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus {
            border-color: #2575fc;
            outline: none;
            box-shadow: 0 0 0 3px rgba(37, 117, 252, 0.1);
        }
        
        .login-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #2575fc 0%, #6a11cb 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(37, 117, 252, 0.4);
        }
        
        .login-btn:active {
            transform: translateY(0);
        }
        
        .login-btn:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }
        
        .error-message {
            background-color: #fee;
            color: #c33;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border-left: 4px solid #c33;
            display: none;
        }
        
        .success-message {
            background-color: #efe;
            color: #363;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border-left: 4px solid #363;
            display: none;
        }
        
        .info-text {
            color: #666;
            font-size: 0.9rem;
            margin-top: 20px;
            line-height: 1.5;
        }
        
        .class-select {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5ee;
            border-radius: 8px;
            font-size: 1rem;
            background: white;
            margin-bottom: 25px;
        }
        
        .class-select:focus {
            border-color: #2575fc;
            outline: none;
            box-shadow: 0 0 0 3px rgba(37, 117, 252, 0.1);
        }
        
        .loading {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid #ffffff;
            border-radius: 50%;
            border-top-color: transparent;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
        }
        
        @keyframes spin {
            to { transform: rotate(360deg); }
        }
        
        @media (max-width: 480px) {
            .login-container {
                padding: 30px 20px;
            }
            
            .logo h1 {
                font-size: 1.7rem;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h1>作业管理系统</h1>
            <div class="subtitle">
                <span class="school-name">越秀外国语学院</span> · 
                <span class="course-name">《人机交互》课程</span>
            </div>
        </div>
        
        <div id="errorMessage" class="error-message">
            学号或姓名不正确，请重新输入
        </div>
        
        <div id="successMessage" class="success-message">
            登录成功，正在跳转...
        </div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="classSelect">选择班级</label>
                <select id="classSelect" class="class-select" required>
                    <option value="">请选择班级</option>
                    <option value="1">数字媒体技术1班</option>
                    <option value="2">数字媒体技术2班</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="studentId">学号</label>
                <input type="text" id="studentId" placeholder="请输入您的学号" required>
            </div>
            
            <div class="form-group">
                <label for="studentName">姓名</label>
                <input type="text" id="studentName" placeholder="请输入您的姓名" required>
            </div>
            
            <button type="submit" class="login-btn" id="loginBtn">
                <span id="loginText">登录系统</span>
                <span id="loginLoading" style="display: none;">
                    <span class="loading"></span>验证中...
                </span>
            </button>
        </form>
        
        <div class="info-text">
            <p>请使用您的真实学号和姓名登录系统</p>
            <p>系统将连接数据库验证您的学生身份信息</p>
        </div>
    </div>

    <script>
        // API基础URL
        const API_BASE_URL = 'http://8.136.185.202/login/students_api.php';
        
        document.getElementById('loginForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const classSelect = document.getElementById('classSelect').value;
            const studentId = document.getElementById('studentId').value.trim();
            const studentName = document.getElementById('studentName').value.trim();
            const errorMessage = document.getElementById('errorMessage');
            const successMessage = document.getElementById('successMessage');
            const loginBtn = document.getElementById('loginBtn');
            const loginText = document.getElementById('loginText');
            const loginLoading = document.getElementById('loginLoading');
            
            // 隐藏消息
            errorMessage.style.display = 'none';
            successMessage.style.display = 'none';
            
            if (!classSelect) {
                showError('请选择班级');
                return;
            }
            
            if (!studentId || !studentName) {
                showError('请输入学号和姓名');
                return;
            }
            
            // 显示加载状态
            loginBtn.disabled = true;
            loginText.style.display = 'none';
            loginLoading.style.display = 'inline';
            
            try {
                // 调用后端API验证登录
                const response = await fetch(`${API_BASE_URL}/students_api.php?action=login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        class: classSelect,
                        studentId: studentId,
                        studentName: studentName
                    })
                });
                
                const result = await response.json();
                
                if (result.success) {
                    // 登录成功
                    showSuccess('登录成功！');
                    
                    // 保存登录信息
                    const loginInfo = {
                        class: classSelect,
                        studentId: studentId,
                        studentName: studentName,
                        loginTime: new Date().toISOString()
                    };
                    
                    localStorage.setItem('hci_login_info', JSON.stringify(loginInfo));
                    
                    // 延迟跳转，让用户看到成功消息
                    setTimeout(() => {
                        window.location.href = 'index.html';
                    }, 1000);
                    
                } else {
                    // 登录失败
                    showError(result.error || '学号或姓名不正确，请重新输入');
                    loginBtn.disabled = false;
                    loginText.style.display = 'inline';
                    loginLoading.style.display = 'none';
                }
                
            } catch (error) {
                console.error('登录请求失败:', error);
                showError('网络错误，请稍后重试');
                loginBtn.disabled = false;
                loginText.style.display = 'inline';
                loginLoading.style.display = 'none';
            }
        });
        
        function showError(message) {
            const errorMessage = document.getElementById('errorMessage');
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }
        
        function showSuccess(message) {
            const successMessage = document.getElementById('successMessage');
            successMessage.textContent = message;
            successMessage.style.display = 'block';
        }
        
        // 输入时隐藏错误信息
        document.getElementById('studentId').addEventListener('input', function() {
            document.getElementById('errorMessage').style.display = 'none';
        });
        
        document.getElementById('studentName').addEventListener('input', function() {
            document.getElementById('errorMessage').style.display = 'none';
        });
        
        document.getElementById('classSelect').addEventListener('change', function() {
            document.getElementById('errorMessage').style.display = 'none';
        });
    </script>
</body>
</html>